<?php
// DBO-backed object data model, for mapping database tables to Cake objects.


class Model extends Object {

    function &get_db() {
        global $aldb;
        static $db = null;
        if (empty($db))
            $db =& $aldb;
        return $db;
    }

    function find_all($conditions = null, $fields = array(), $order = null) {
        $db = &$this->get_db();
        if (empty($fields) || !is_array($fields))
            $fields = '*';
        else
            $fields = implode(',', $fields);
        if (empty($conditions)) {
            $conditions = '';
        } else {
            $conditions = "WHERE $conditions";
        }
        return $db->get_results("SELECT $fields FROM " . $this->table_name() . " $conditions $order");
    }
/*
find 
  string $conditions 
  array $fields 
  string $order 
  int $recursive 
  */
    function find($conditions, $fields = array(), $order = null) {
        $db = &$this->get_db();
        if (empty($fields) || !is_array($fields)) {
            $fields = '*';
        } else {
            $fields = implode(',', $fields);
        }
        return $db->get_row("SELECT $fields FROM " . $this->table_name() . " WHERE $conditions $order LIMIT 1");
    }

    function find_allBy() {
        
    }

    function find_count() {
        
    }

    function read($fields = null, $id = null) {
        $db = &$this->get_db();
        if (empty($id)) {
            $id = $this->id;
        }
        if ($id === null)
            return false;
        if (empty($fields))
            $fields = '*';
        return $db->get_row("SELECT $fields FROM " . $this->table_name() . " WHERE id='$id' LIMIT 1");
    }

/*
  string $name 
  string $conditions 
  string $order 
*/
    function field($name, $conditions, $order = null) {
        $db = &$this->get_db();
        return $db->get_var("SELECT `$name` FROM " . $this->table_name() . " WHERE $conditions $order LIMIT 1");
    }

    function query($query) {
        $db = &$this->get_db();
        return $db->query($query);
    }

    function del($id) {
        $db = &$this->get_db();
        $db->query("DELETE FROM " . $this->table_name() . " WHERE id='$id' LIMIT 1");
    }

    function del_all($conditions) {
        $db = &$this->get_db();
        $db->query("DELETE FROM " . $this->table_name() . " WHERE $conditions");
    }
/*
save_field 
  string $name 
  string $value 
*/

    function save_field($name, $value, $conditions = null) {
        if (!$this->has_field($name)) {
            return false;
        }
        $db = &$this->get_db();
        if (empty($conditions)) {
            $conditions = "WHERE `id`='$this->id'";
        } else {
            $conditions = "WHERE $conditions";
        }
        $db->query("UPDATE `" . $this->table_name() . "` SET `$name`='$value' $conditions LIMIT 1");
    }

    function save($data) {
        $db = &$this->get_db();
        if (empty($data[$this->name]['id'])) {
            $query = 'INSERT INTO ' . $this->table_name() . ' ( ';
            $query2 = ' VALUES ( ';
            if (isset($data[$this->name]) && is_array($data[$this->name])) {
                foreach($data[$this->name] as $key => $value) {
                    if ($this->has_field($key)) {
                        $query .= '`' . $key . '`' . ', ';
                        $query2 .= '\'' . $value . '\', ';
                    }
                }
            }
            $query = rtrim($query, ', ') . ' )';
            $query2 = rtrim($query2, ', ') . ' )';

            $db->query($query . $query2);
            $this->last_insert_id = $db->insert_id;
        } else {
            $id = $data[$this->name]['id'];
            $query = 'UPDATE `' . $this->table_name() . '` SET ';
            foreach($data[$this->name] as $key => $value) {
                if ($this->has_field($key)) {
                    $query .= '`' . $key . '`' . '=\'' . $value . '\', ';
                }
            }
            $query = rtrim($query, ', ');
            $query .= " WHERE `id`='$id' LIMIT 1";
            $db->query($query);
            $this->last_insert_id = $id;
        }
    }

    function get_count($conditions, $field = '*') {
        $db = &$this->get_db();
        if (!empty($conditions)) {
            $conditions = "WHERE $conditions";
        }
        return $db->get_var("SELECT COUNT($field) FROM `" . $this->table_name() . "` $conditions");
    }

    function get_last_insert_id() {
        if (empty($this->last_insert_id)) {
            $db = &$this->get_db();
            $this->last_insert_id = $db->insert_id;
        }
        return $this->last_insert_id;
    }


    function table_name() {
        $db = &$this->get_db();
        $name = strtolower($this->name);
        return $db->$name;
    }

    function has_field($name) {
        if (is_array($name)) {
            foreach ($name as $n) {
                if ($this->has_field($n)) {
                    return $n;
                }
            }
            return false;
        }

        if (empty($this->_schema)) {
            $this->schema();
        }

        if ($this->_schema != null) {
            return isset($this->_schema[$name]);
        }
        return false;
    }

    function schema() {
        global $aldb;
        if(empty($this->_schema)) {
            $this->_schema = array();
            $results = $aldb->get_results('DESCRIBE `' . $this->table_name() . '`', ARRAY_A);
            foreach($results as $result) {
                $this->_schema[$result['Field']] = true;
            }
        }
    
    }

}

?>
